<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller{
	var $_public_view;

	function __construct(){
		parent::__construct();
		$this->load->model('model_perjalanan', 'mtrip');
        $this->load->helper('url');
        $this->load->helper('string');
        $this->load->library('session');
        $this->config->load('ojexcom');
        $this->_public_view = $this->config->item('public_view');
	}

	function index(){
		//cek session
        $isLogin = $this->session->userdata('isLogin');
        $user = ''; $id='';
        if ($isLogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

		$username = $this->input->post('username');
		$password = $this->input->post('password');
		if($username != NULL){
			$query = $this->db->query("SELECT `id`, name, isPremium FROM `user` WHERE username='".$username."' AND password='".md5($password)."'");
			$row = $query->row();
			if($query->num_rows() > 0){
				$data_output['success'] = "true";
				$data_output['id'] = $row->id;
				$data_output['name'] = $row->name;
				$data_output['isPremium'] = $row->isPremium."";

				//set session
		        $this->session->set_userdata('isLogin', true);
		        $this->session->set_userdata('username', $username);
		        $this->session->set_userdata('user_id',$row->id);
		        $this->session->set_userdata('isPremium',$row->isPremium);
			} else {
				$data_output['success'] = "false";
				//$this->session->set_userdata('page','login');
				$data = array('page_content' => 'login', 'page_title' =>'LOG IN','isLogin'=>'',
					'error'=>'username atau password salah');
				$this->load->view($this->_public_view, $data);
			}
			
			$ios_token = $this->input->post('ios_token');
			if($ios_token != NULL) {
				$input_data = array(
					'ios_token' => $ios_token
				);
				$this->db->where('id', $row->id);
				$this->db->update('user', $input_data);
			}
		} else {
			$data_output['success'] = "false";
			//$this->session->set_userdata('page','login');
			$data = array('page_content' => 'login', 'page_title' =>'LOG IN',
				'error' => 'username dan password harus diisi',
				'isLogin'=>'');
			$this->load->view($this->_public_view, $data);
		}		

		if($data_output['success']=="true" and $this->session->userdata('page')){
			redirect($this->session->userdata('page'));
		}

		if($data_output['success']=="true"){
			$id = $this->session->userdata('user_id');
			$this->session->unset_userdata('page');
			$data = array('page_content' => 'home', 'page_title' =>'O-JEX',            
	            		'output' => $this->mtrip->get(),
	                    'user' => $this->session->userdata('username'), 
	                    'isLogin' => $this->session->userdata('isLogin'),
	                    'id'=>$this->session->userdata('user_id'));
			$this->load->view($this->_public_view, $data); //tampilkan datanya
		}
	}


}